var param = JSON.parse(localStorage.getItem('param'));
var productId = param.productId;
var userId = localStorage.getItem('userid');
var type = param.type;
$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	//初始化商品信息
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/product/getTeaProductInfo.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			productId:productId,
			userId:userId
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				var prodPic = result.data.productPic;
				var prod = result.data;
				if(prodPic){
					//获取到图片对象
					console.log(prodPic);
					var coverPic = prodPic.coverPic;//封面图
					var coverPics = coverPic.split(",");
					var upStr = '';
					$.each(coverPics,function(i,n){
						upStr += '<div class="swiper-slide"><img src="'+n+'" /></div>';
					});
					$('.swiper-wrapper').html(upStr);
					
					var pics = getPics(prodPic);
					var downStr = '<div class="comment-normal"><span>详情</span></div>';
					$.each(pics,function(i,n){
						downStr += '<div class="img-detail"><img src="'+n+'" alt="" /></div>';
					});
					$('#proDetail').html(downStr);
				}
				var teaIndustryStr = ''
				var teaIndustrys = prod.teaIndustry.split(",");
				$.each(teaIndustrys,function(i,n){
					if(n==0){
						teaIndustryStr += '<span class="fl color02">茶艺师</span>';
					}else if(n==1){
						teaIndustryStr += '<span class="fl color03">评茶员（师）</span>';
					}else if(n==2){
						teaIndustryStr += '<span class="fl color02">制茶师</span>';
					}else if(n==3){
						teaIndustryStr += '<span class="fl color03">茶园园艺工</span>';
					}
				});
				var trip;
				if(prod.teaTrip=='0'){
					trip = '不出差';
				}else{
					trip = '可出差';
				}
				var str = '<div class="item-det clear"><span class="fl name">'+prod.productname+'</span>'+teaIndustryStr+'<p class="fr">女/'+prod.teaAge+'岁/从业'+prod.teaYear+'年/'+trip+'</p></div><div class="item-time clear"><span class="fl">成交量：'+prod.teaSellNum+'</span><span class="fr">月薪面议</span></div><div class="num-box clear"><div class="fl">￥<span>'+prod.priceretailguide+'</span>元</div><p class="fr">'+prod.teaLocation+'</p></div>';
				$('.skill-box').html(str);
				var mySwiper1 = new Swiper('.swiper-container2', {
					direction: 'horizontal',
					loop: true,
				});
			}else{
				showErrorMsg(result.data);
			}
		}
	});
	//初始化评论
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/comment/getCommentList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			orderId:productId,
			type:"part"
		},
		success : function(result) {
			var str = '<div class="comment-normal"><span>评价</span></div>';
			if (result.retCode == '0000') {
				$.each(result.data,function(i,n){
					var id = n.id||'';
					var userId = n.userid||'';
					var userName = n.username||'';
					var orderId = n.orderid||'';
					var pics = n.pics.split(',');
					var imgStr = '';
					if(pics&&pics!=''){
						imgStr = '<div class="comment-img clear">';
						$.each(pics,function(j,m){
							imgStr +='<div class="img-box fl"><img src="'+m+'" alt="" /></div>';
						});
						imgStr +='</div>';
					}
					var msg = n.msg||'';
					var addTime = n.addtime.substring(0,10)||'';
					str +='<div class="comment-list"><div class="comment-item clear"><span class="fl comment-name">'+userName+'</span><span class="fr comment-time">'+addTime+'</span></div><p class="comment-con">'+msg+'</p>'+imgStr+'</div>';
				});
				if(result.data.length>0){
					str += '<div class="comment-btn"><span>查看全部评价</span></div>';
				}
			}
			$('#commentList').html(str);
		}
	});
	//查看用户是否收藏该商品
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/user/collect/checkCollect.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			userId:userId,
			productId:productId
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				if(result.data){
					$('#teaCollect').addClass("collect-sel");
					$('.comment-btn-z').attr('collectid',result.data);
				}
			}else{
				showErrorMsg(result.data);
			}
		}
	});
}
//初始化绑定事件
function initButton(){
	//购买按钮
	$('.buy-box').on('click',function(){
		var param = {};
		param.productId = productId;
		param.type = type;//为了订单类型区分
		//跳转
		localStorage.setItem('param',JSON.stringify(param));
		window.location = server_url + "buyNow.do";
	});
	$('#chat').on('click',function(){
		window.location = server_url + "tea/views/chat/chat.html";
	});
	//收藏
	$('.comment-btn-z').on('click', function() {
		$(this).children('p').toggleClass('collect-sel');
		var collect = $(this);
		var collectId = $(this).attr('collectid');
		if(!(collectId=="")){
			//原先是收藏的，现在点是为了取消收藏
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/user/collect/deleteCollect.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					id:collectId
				},
				success : function(result) {
					collect.attr('collectid','');
				}
			});
		}else{
			//原先没有收藏现在是在收藏
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/user/collect/addCollect.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					productId:productId,
					userId:localStorage.getItem('userid')
				},
				success : function(result) {
					collect.attr('collectid',result.data.id);
				}
			});
		}
	})
}